package com.hax2max.view;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorTreeAdapter;
import com.hax2max.R;
import com.hax2max.control.UpdateExternalDataStatusThread;
import com.hax2max.model.ExternalData;
import com.hax2max.model.Profile;


/**
 * Activity used for listing all friends in friends list.
 *
 */
public class ListViewController extends ExpandableListActivity implements OnClickListener{


    private MenuViewController menu;
    private Context mContext;
    private Button updateButton;
    private Button manageButton;
    private Cursor c;
    private ProgressDialog updatingDialog;
    private UpdateExternalDataStatusThread updatingThread;
    private Handler handler;
    private MenuViewController menuViewController;

    
    
    
    
    public void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.list);
	mContext = getBaseContext();
	if (menuViewController == null)
	    menuViewController = MenuViewController.getInstance(mContext);
	updateButton = (Button)findViewById(R.id.ListViewButtonUpdate);
	manageButton = (Button)findViewById(R.id.ListViewButtonManage);
	mContext = getBaseContext();
	displayAllFriends();
	updateButton.setOnClickListener(this);
	manageButton.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			
			startActivity(new Intent(mContext, ManageFriendsViewController.class));
		}
	});

	handler = new Handler() {
	    public void handleMessage(Message msg) {
		switch(msg.what){
		case UpdateExternalDataStatusThread.PROGRESS:
		    updatingDialog.setProgress(msg.getData().getInt("progress"));
		    break;
		case UpdateExternalDataStatusThread.DIALOGMESSAGE:
		    updatingDialog.setMessage(mContext.getString(R.string.ListViewUpdateDialogMessageSave));
		    break;
		case UpdateExternalDataStatusThread.MAX:
		    updatingDialog.setMax(msg.getData().getInt("max"));
		    break;
		case UpdateExternalDataStatusThread.DONE:
		    if(msg.getData().getBoolean("done"))
			    dismissDialog(0);
			removeDialog(0);
			displayAllFriends();
		    break;
		}
	    }
	};

    }

    /**
     * Binds cursor adapter to the view which contains all friends
     */
    private void displayAllFriends()
    {
	// group items from the exandable list
	String[] from = {
		Profile.fnameColumn,
		Profile.lnameColumn,
		Profile.lastupdatedColumn,
		"icon"+ExternalData.ApplicationIDFriendStalker,
		"aid"+ExternalData.ApplicationIDFriendStalker,
		"icon"+ExternalData.ApplicationIDFacebook,
		"aid"+ExternalData.ApplicationIDFacebook,
		"icon"+ExternalData.ApplicationIDTwitter,
		"aid"+ExternalData.ApplicationIDTwitter};

	int[] to = {
		R.id.ViewListFname,
		R.id.ViewListLname,
		R.id.ViewListLastUpdated,
		R.id.ViewListFriendStalkerIcon,
		R.id.ViewListFriendStalkerStatus,
		R.id.ViewListFacebookIcon,
		R.id.ViewListFacebookStatus,
		R.id.ViewListTwitterIcon,
		R.id.ViewListTwitterStatus
	};
	// child items for the expandable list
	String[] childFrom = {
		Profile.usernameColumn,
		Profile.emailColumn,
		Profile.mobileColumn,
		Profile.homepageColumn };

	int[] childTo = {
		R.id.ViewListUserName,
		R.id.ViewListEmail,
		R.id.ViewListMobile,
		R.id.ViewListHomepage
	};


	c = menuViewController.dataHandler.getFriendsListCursorAllData(null);
	startManagingCursor(c);
	SimpleCursorTreeAdapter adapter = 
	    /*
	     * private inner class
	     */
	    new SimpleCursorTreeAdapter(this, c, R.layout.listfriendrow, R.layout.listfriendrow, 
		    from, to, R.layout.listfriendchildrow, childFrom, childTo)
	{
	    @Override
	    protected Cursor getChildrenCursor(Cursor groupCursor) 
	    {
		return menuViewController.dataHandler.getProfileCursor(
			groupCursor.getString(
				groupCursor.getColumnIndex(
					Profile.usernameColumn)));
	    }
	};

	setListAdapter(adapter);
    }

    public void onClick(View v)

    {
	Log.e("ListView", "clicked button");
	showDialog(0);

    }


    protected Dialog onCreateDialog(int id)
    {
	updatingDialog = new ProgressDialog(this);
	updatingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	updatingDialog.setTitle(mContext.getString(R.string.ListViewUpdateDialogHead)); 
	updatingDialog.setMessage(mContext.getString(R.string.ListViewUpdateDialogMessage));
	updatingThread = new UpdateExternalDataStatusThread(handler,menuViewController.dataHandler);
	updatingThread.start();
	return updatingDialog;
    }



    public void onDestroy(){
	c.close();
	super.onDestroy();
    }

    public void onStop(){

	finish();
	super.onStop();
    }

    public boolean onCreateOptionsMenu(Menu menu) {

	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.menu.menu, menu);
	return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
	menuViewController.start(this,item);
	return true;
    }
} 

